<!--
 * @Author: weikang
 * @Description:
 * @Date: 2023-12-23 11:38:28
 * @LastEditTime: 2025-01-26 17:17:48
 * @FilePath: /env-manager-pc/src/views/update/version/index.vue
-->
<script setup lang="ts">
import { ElMessage } from 'element-plus'
import { useDeviceStates, useOwnerOrgs } from '@/utils/dataHook'
import { usePage } from '@/utils/commonHook'
import updateApi from '@/api/modules/update'
import type { DeviceRow } from '@/api/modules/types/document'

defineOptions({
  name: 'UpdateVersion',
})
const router = useRouter()

const tableRef = ref()
const searchData = reactive({
  'DIM.Devices.Filters.AssetCodeFilter': '',
  'DIM.Devices.HJ212.DeviceFilters.MNFilter': '',
  'DIM.Devices.Filters.OwnerOrgGUIDFilter': '',
  'DIM.Devices.Filters.StateFilter': [],
  'DIM.Business.DeviceFilters.EntNameFilter': '',

})
const { page, pageNumChange, pageSizeChange } = usePage(getTableData)

const OwnerList = useOwnerOrgs()
const runStatusList = useDeviceStates()
const defaultProps = {
  children: 'Children',
  label: 'Name',
}

function search() {
  page.PageNum = 1
  getTableData()
}
const tableData = ref<DeviceRow[]>([])
const loading = ref(false)

async function getTableData() {
  loading.value = true
  const res = await updateApi.GetLastUpgrades({
    objFilter: {
      Type: 'DIM.FilterCollection',
      LogicOperator: 0,
      Filters: [
        {
          Type: 'DIM.Devices.Filters.AssetCodeFilter',
          IsFuzzy: true,
          Value: [searchData['DIM.Devices.Filters.AssetCodeFilter']],
        },
        {
          Type: 'DIM.Devices.HJ212.DeviceFilters.MNFilter',
          Value: [searchData['DIM.Devices.HJ212.DeviceFilters.MNFilter']],
        },
        {
          Type: 'DIM.Devices.Filters.OwnerOrgGUIDFilter',
          Value: [searchData['DIM.Devices.Filters.OwnerOrgGUIDFilter']],
        },
        {
          Type: 'DIM.Devices.Filters.StateFilter',
          Value: searchData['DIM.Devices.Filters.StateFilter'],
        },
        {
          Type: 'DIM.Business.DeviceFilters.EntNameFilter',
          Value: [searchData['DIM.Business.DeviceFilters.EntNameFilter']],
        },
      ],
    },
    pageSize: page.PageSize,
    pageNum: page.PageNum,
  })
  loading.value = false
  // console.log('%c [ res ]-27', 'font-size:13px; background:pink; color:#bf2c9f;', res)
  const { Rows, RecordsetCount } = res
  tableData.value = Rows || []
  page.RecordsetCount = RecordsetCount
}

function toEnt(row: any) {
  router.push(`/document/manager/modify/${row.EntObjID}`)
}

const callLoading = ref(false)

function checkUpdate() {
  const rows = tableRef.value.getCheckboxRecords()
  if (rows.length <= 0) {
    return ElMessage.warning('请勾选巡检数据')
  }
  callLoading.value = true
  updateApi.CallVersions({ deviceObjIDs: rows.map((item: any) => item.DeviceObjID) }).then((res) => {
    Object.keys(res).forEach((key) => {
      const row = tableData.value.find((item: any) => item.DeviceObjID === key)
      if (row) {
        Object.assign(row, res[key] || {})
      }
    })
    callLoading.value = false
  }, () => {
    callLoading.value = false
  })
}

getTableData()
</script>

<template>
  <div>
    <PageHeader>
      <template #content>
        <ElForm label-width="100">
          <ElRow>
            <ElCol :span="6">
              <ElFormItem label="设备编码">
                <ElInput v-model="searchData['DIM.Devices.Filters.AssetCodeFilter']" clearable placeholder="请输入" />
              </ElFormItem>
            </ElCol>
            <ElCol :span="6">
              <ElFormItem label="MN号">
                <ElInput v-model="searchData['DIM.Devices.HJ212.DeviceFilters.MNFilter']" clearable placeholder="请输入" />
              </ElFormItem>
            </ElCol>
            <ElCol :span="6">
              <ElFormItem label="运维公司">
                <el-tree-select
                  v-model="searchData['DIM.Devices.Filters.OwnerOrgGUIDFilter']"
                  value-key="Code"
                  class="w-full"
                  :data="OwnerList"

                  :render-after-expand="false"
                  :props="defaultProps"
                  clearable check-strictly
                />
              </ElFormItem>
            </ElCol>
            <ElCol :span="6">
              <ElFormItem label="运行状态">
                <ElSelect v-model="searchData['DIM.Devices.Filters.StateFilter']" multiple class="w-full" clearable placeholder="请选择">
                  <ElOption v-for="item in runStatusList" :key="item.StateID" :label="item.Name" :value="item.StateID" />
                </ElSelect>
              </ElFormItem>
            </ElCol>
            <ElCol :span="6">
              <ElFormItem label="企业名称">
                <ElInput v-model="searchData['DIM.Business.DeviceFilters.EntNameFilter']" clearable placeholder="请输入" />
              </ElFormItem>
            </ElCol>
            <ElCol :span="6" :push="12" class="flex flex-justify-end">
              <ElButton type="primary" @click="search">
                查询
              </ElButton>
              <!-- <ElButton type="warning">
                导出
              </ElButton> -->
              <ElButton type="success" :loading="callLoading" @click="checkUpdate">
                巡测
              </ElButton>
            </ElCol>
          </ElRow>
        </ElForm>
      </template>
    </PageHeader>
    <PageMain>
      <div v-auto-height="85">
        <vxe-table
          ref="tableRef"
          height="100%" auto-resize
          border
          :data="tableData"
          :loading="loading"
          :row-config="{ isCurrent: true }"
        >
          <VxeColumn title="" type="checkbox" width="80" />
          <VxeColumn title="序号" type="seq" width="80" />
          <!-- <VxeColumn field="OwnerOrgClassifyName" title="类型" min-width="100" /> -->
          <VxeColumn field="OwnerOrgName" title="运维公司" min-width="150" show-overflow />
          <VxeColumn field="EntName" title="企业名称" min-width="220" show-overflow>
            <template #default="{ row }">
              <el-button
                type="primary"
                link
                @click="toEnt(row)"
              >
                {{ row.EntName }}
              </el-button>
            </template>
          </VxeColumn>
          <VxeColumn field="DeviceName" title="设备名称" min-width="220" show-overflow />
          <VxeColumn field="DeviceAsset" title="设备编码" min-width="220" />
          <VxeColumn field="MN" title="MN号" min-width="220" show-overflow />
          <VxeColumn field="PointCode" title="采集因子" min-width="100" />
          <VxeColumn field="UpgradeState" title="运行状态" width="100" />
          <VxeColumn field="SoftVer" title="软件版本号" width="130" show-overflow />
          <VxeColumn field="HardVer" title="硬件版本号" width="130" show-overflow />
          <VxeColumn field="UpLastEndTime" title="最后采集时间" width="130" />
        </vxe-table>
      </div>
      <ElPagination
        class="mt-4"
        :page-size="page.PageSize"
        :total="page.RecordsetCount"
        :current-page="page.PageNum"
        layout="total, sizes, prev, pager, next, jumper"
        :page-sizes="[20, 50, 100]"
        @current-change="pageNumChange"
        @size-change="pageSizeChange"
      />
    </PageMain>
  </div>
</template>
